import { Vector3 } from "three";
import { parseVector3 } from "./utils";

export function fract(x) {
    return x - Math.floor(x);
};


/**
 * 获取从属方向
 * @param alpha 
 * @param beta 
 * @returns 
 */
export function directionFromAlphaBeta(alpha, beta) {
    // 90 度在某些处理会出问题，直接使用近似值好了
    if (alpha == 90) {
        alpha = 89.999999;
    }
    const theta = alpha / 180 * Math.PI + Math.PI / 2;
    const phi = -beta / 180 * Math.PI + Math.PI / 2;

    const dir = new Vector3();
    const r = Math.sin(theta);
    dir.x = r * Math.cos(phi);
    dir.y = -Math.cos(theta);
    dir.z = r * Math.sin(phi);

    return dir.toArray();
}

export function getVectorLength(vector) {
    const [x, y, z] = vector;
    return Math.sqrt(x * x + y * y + z * z);
}

export function normalizeVector(vector) {
    const [x, y, z] = vector;
    const length = 1.0 / getVectorLength(vector);
    return [x * length, y * length, z * length];
}

export function getDistance(v1, v2) {
    return parseVector3(v1).distanceTo(parseVector3(v2));
};

export function get2DDistance(v1, v2){
    return new Vector3(v1[0],0,v1[1]).distanceTo(new Vector3(v2[0],0,v2[1]));
}